package com.cloudxingkong.dianyinke.Student.Adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Adapter.DTGVAdapter;
import com.cloudxingkong.dianyinke.NewTeacher.Adapter.ZanGVAdapter;
import com.cloudxingkong.dianyinke.NewTeacher.Ui.ImagePagerActivity;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.CommentListView;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.NoScrollGridView;
import com.cloudxingkong.dianyinke.NewTeacher.bean.Likes;
import com.cloudxingkong.dianyinke.NewTeacher.config.Constans;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Class.MyGridView;
import com.cloudxingkong.dianyinke.Student.bean.StudentDt;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 云星空科技 on 2016/3/22.
 */
public class TeacherDongTaiAdapter extends BaseAdapter {
    private List<StudentDt.ListBean> list;
    private Context context;
    private getOnTouchDTPosition mPosition;

    public TeacherDongTaiAdapter(List<StudentDt.ListBean> list, Context context,getOnTouchDTPosition mPosition) {
        this.mPosition=mPosition;
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView=View.inflate(context, R.layout.fragment_teacher_ptitude_item,null);
            holder=new ViewHolder();
            holder.content= (TextView) convertView.findViewById(R.id.tp_tv_content);
            holder.name= (TextView) convertView.findViewById(R.id.tp_tv_name);
            holder.rangeTime= (TextView) convertView.findViewById(R.id.tp_tv_rangetime);
            holder.time= (TextView) convertView.findViewById(R.id.tp_tv_time);
            holder.gridView= (NoScrollGridView) convertView.findViewById(R.id.tpi_gridview);
            holder.listView = (CommentListView) convertView.findViewById(R.id.tp_listview_comm);
            holder.image_popu_zan = (ImageView) convertView.findViewById(R.id.image_popu_zan);
            holder.zanGridview= (MyGridView) convertView.findViewById(R.id.tp_gridview_zan);
            holder.touxiang = (CircleImageView) convertView.findViewById(R.id.tp_iv_touxiang);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        if (list == null) {
            Log.e("test","数据为空");
        }else{
            mPosition.getPosition(position);
            /**
             * 主要内容
             */
            holder.name.setText(list.get(position).getMemname());
            holder.content.setText(list.get(position).getText());
            holder.time.setText(list.get(position).getTs() + ".02.13");
            holder.rangeTime.setText(list.get(position).getDr() + "小时前");
            ImageLoader.getInstance().displayImage(list.get(position).getAvatar(), holder.touxiang);

            int id = list.get(position).getId();

            holder.gridView.setTag(id);

            boolean b = list.get(position).getPics().isEmpty();
            if (b==false){
                if (holder.gridView.getTag()!=null&&holder.gridView.getTag().equals(id)){

                    String pics = list.get(position).getPics();
                    final String[] split = pics.split(",");
                    if (!pics.equals("")){
                        if (pics.indexOf(",")==-1) {
                            split[0]=pics;
                            holder.gridView.setVisibility(View.VISIBLE);
                            holder.gridView.setAdapter(new DTGVAdapter(split, context));
                            holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    imageBrower(position,split);
                                }
                            });

                        }else{
                            holder.gridView.setVisibility(View.VISIBLE);
                            holder.gridView.setAdapter(new DTGVAdapter(split, context));
                            holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    imageBrower(position, split);
                                }
                            });
                        }
                    }else{
                        holder.gridView.setVisibility(View.GONE);
                    }
                    /**
                     * 评论点赞
                     */

                    holder.commList.clear();
                    holder.zanList.clear();
                    if (list.get(position).getDetail() != null) {
                        List<StudentDt.ListBean.DetailBean> detail = list.get(position).getDetail();

                        for (StudentDt.ListBean.DetailBean bean : detail) {
                            if (bean.getType().equals(Constans.TYPE_COMMENT)) {
                                StuGetDTDetailCommAdapter adapter = new StuGetDTDetailCommAdapter(holder.commList, context);
                                holder.listView.setAdapter(adapter);
                                holder.commList.add(bean);
                                adapter.notifyDataSetChanged();
                            }
                            if (bean.getType().equals(Constans.TYPE_ZAN)) {
                                ZanGVAdapter zanGVAdapter = new ZanGVAdapter(holder.zanList, context);
                                holder.zanGridview.setAdapter(zanGVAdapter);

                                int id1 = bean.getId();
                                String zanAvatar = bean.getAvatar();
                                Log.e("test", "获取老师动态的赞 " + zanAvatar);
                                final String memname = bean.getMemname();
                                Likes likes = new Likes(zanAvatar, id1, memname);
                                likes.setUserMemename(memname);
                                likes.setUserId(id1);
                                likes.setUserAvatar(zanAvatar);
                                holder.zanList.add(likes);
                                zanGVAdapter.notifyDataSetChanged();
                            }
                        }
                    }


                }
            }else{
                holder.gridView.setVisibility(View.GONE);
            }

            int dynamicid  = list.get(position).getId();
            final ViewHolder finalHolder = holder;
            mPosition.getMore(holder.image_popu_zan, position);


            final ViewHolder finalHolder1 = holder;
            holder.zanGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String memename = finalHolder1.zanList.get(position).getUserMemename();
                    Toast.makeText(context, memename, Toast.LENGTH_SHORT).show();

                }
            });
        }

        return convertView;
    }
    private class ViewHolder{
        CircleImageView touxiang;
        TextView name,time,content,rangeTime;
        NoScrollGridView gridView;
        ImageView image_popu_zan;
        CommentListView listView;
        MyGridView zanGridview;
        List<Likes> zanList=new ArrayList<>();
        List<StudentDt.ListBean.DetailBean> commList=new ArrayList<>();
    }

    public void clear(){
        list.clear();
        notifyDataSetChanged();
    }
    public interface getOnTouchDTPosition {
        void getPosition(int position);
        void getMore(ImageView iv,int position);
    }
    //图片点击放大，轮滑
    private void imageBrower(int position, String[] pics) {
        Intent intent = new Intent(context, ImagePagerActivity.class);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, pics);
        context.startActivity(intent);

    }
}
